package toDo.app.controllers;

import toDo.app.R;
import toDo.app.models.User;
import database.DatabaseFacade;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import interfaces.ILoginActivity;

public class LoginActivityClickController implements OnClickListener {
	ILoginActivity activity;

	public LoginActivityClickController(ILoginActivity activity) {
		this.activity = activity;
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
			case R.id.loginLoginButton:
				processLoginEvent();
				break;
			case R.id.loginBackButton:
				processBackEvent();
				break;
		}
	}

	public void processLoginEvent() {
		User user = new User(activity.getUsernameText(), activity.getPasswordText());
		if (DatabaseFacade.isValidUser(activity, user).isValid()) {
			user = DatabaseFacade.isValidUser(activity, user);
			// statusLabel.setText("Valid");
			// new
			// AlertDialog.Builder(this).setTitle("Success!").setMessage("The user's login info is valid.").setNeutralButton("OK",
			// null).show();
			activity.setResult(ILoginActivity.RESULT_OK, User.putUserIntoIntent(new Intent(), user));
			activity.finish();
		} else {
			// statusLabel.setText("Invalid");
			new AlertDialog.Builder(activity).setTitle(R.string.error).setMessage(R.string.loginFailed)
					.setNeutralButton(R.string.ok, null).show();
			activity.setUsernameText(activity.getString(R.string.blank));
			activity.setPasswordText(activity.getString(R.string.blank));
		}
	}

	public void processBackEvent() {
		activity.setResult(ILoginActivity.RESULT_OK);
		activity.finish();
	}

}
